<template>
  <div class="register-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <el-form-item prop="username">
        <el-input
          ref="username"
          name="username"
          type="text"
          placeholder="请输入姓名"
          maxlength="5"
          v-model="loginForm.username"
        >
          <template slot="prepend">
            <i class="el-icon-user"></i>
          </template>
        </el-input>
      </el-form-item>

      <el-form-item prop="mobile">
        <el-input
          ref="mobile"
          name="mobile"
          type="text"
          maxlength="11"
          placeholder="请输入手机号"
          v-model="loginForm.mobile"
        >
          <template slot="prepend">
            <i class="el-icon-mobile-phone"></i>
          </template>
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          ref="mobile"
          name="mobile"
          type="password"
          placeholder="请输入密码"
          v-model="loginForm.password"
        >
          <template slot="prepend">
            <i class="el-icon-lock"></i>
          </template>
        </el-input>
      </el-form-item>

      <el-form-item prop="confirm_password">
        <el-input
          ref="mobile"
          name="mobile"
          type="password"
          placeholder="请确认密码"
          v-model="loginForm.confirm_password"
        >
          <template slot="prepend">
            <i class="el-icon-lock"></i>
          </template>
        </el-input>
      </el-form-item>

      <el-button
        class="loginBtn"
        type="primary"
        style="width: 100%; margin-bottom: 20px"
        @click.native.prevent="handleLogin"
        >注册账号</el-button
      >
      <div class="tips">
        <el-link
          type="primary"
          :underline="false"
          style="margin-right: 20px"
          @click="$router.push('/login')"
          >已有账号，点击登录</el-link
        >
      </div>
    </el-form>
  </div>
</template>

<script>
import { registerUserApi } from '@/api/user'
export default {
  name: 'register-page',
  data () {
    return {
      loginForm: {
        username: '',
        mobile: '',
        password: '',
        confirm_password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入真实名字', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        mobile: [
          {
            required: true,
            message: '请输入 11 位手机号',
            trigger: 'blur'
          },
          {
            pattern: /^1\d{10}$/,
            message: '请输入正确格式的手机号',
            trigger: ['blur', 'change']
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            message: '密码长度至少 6 位',
            trigger: ['blur', 'change']
          }
        ],
        confirm_password: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          {
            min: 6,
            message: '密码长度至少 6 位',
            trigger: ['blur', 'change']
          }
        ]
      }
      // loading: false
      // passwordType: 'password',
      // redirect: undefined
    }
  },
  methods: {
    async registerUser () {
      const { data: res } = await registerUserApi(this.loginForm)
      if (res.code === 204) {
        this.$message({
          showClose: true,
          message: '用户已存在,请直接登录!',
          type: 'error',
          duration: 1500
        })
      }
      if (res.code === 200) {
        this.$message({
          showClose: true,
          message: '账号注册成功!',
          type: 'success',
          duration: 1500
        })
      }
      this.$router.push('/login')
    },
    // 点击注册账号 表单验证和清空表单
    handleLogin () {
      if (this.loginForm.password !== this.loginForm.confirm_password) {
        return this.$message({
          showClose: true,
          message: '两次输入的密码不一致!',
          type: 'error',
          duration: 1500
        })
      }
      this.$refs.loginForm.validate(valid => {
        // 表单验证没通过则返回
        if (!valid) return
        // this.loading = true
        this.registerUser(this.loginForm)
      })
    }
  }
}
</script>

<style lang="scss">
.register-container {
  background: url('../../assets/热气球.jpg') no-repeat;
  background-size: cover;
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 300px 35px;
    margin: 0 auto;
    overflow: hidden;
  }
  .tips {
    font-size: 14px;
    margin-bottom: 10px;
    .el-link {
      color: white;
    }
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
}
</style>
